package com.e3.sysmg.security.client;

import java.util.List;

import com.e3.sysmg.security.model.ModuleModel;
import com.e3.web.core.model.ModelHelper;
import com.e3.web.widget.client.MyDialog;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;

public class ModuleDetailGridDialog extends MyDialog {
	
	private static ModuleMessages MESSAGES = ModuleMessages.INSTANCE;
	
	private ModelHelper<ModuleModel> modelHelper = new ModelHelper<ModuleModel>();
	
	private Grid<ModuleModel> moduleGridPanel;
	private GridSelectionModel<ModuleModel> gridSelectionModel = new GridSelectionModel<ModuleModel>();
	private ListStore<ModuleModel> moduleGridStore = new ListStore<ModuleModel>();
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		setLayout(new FitLayout());
		this.setSize(600, 450);
		this.setHeading(MESSAGES.title_module_detail());
		add(getModuleGrid());
	} ;

	private Grid<ModuleModel> getModuleGrid() {
		if (moduleGridPanel == null) {
			List<ColumnConfig> configs = ModuleHelper.INSTANCE.getModuleGridColumnConfigs();
			ColumnModel cm = new ColumnModel(configs);
			moduleGridStore.setModelComparer(modelHelper.getModelComparer());
			moduleGridStore.setKeyProvider(modelHelper.getModelKeyProvider());
			moduleGridPanel = new Grid<ModuleModel>(moduleGridStore, cm);
			moduleGridPanel.setSelectionModel(gridSelectionModel);
		}
		return moduleGridPanel;
	}
	
	public void setModules(List<ModuleModel> moduleModels){
		moduleGridStore.removeAll();
		moduleGridStore.add(moduleModels);
	}
	
	public void addModules(List<ModuleModel> moduleModels){
		moduleGridStore.add(moduleModels);
	}

	public List<ModuleModel> getModules(){
		return moduleGridStore.getModels();
	}
	
	public void showDialog(List<ModuleModel> modules){
		moduleGridStore.removeAll();
		if(modules!=null){
			moduleGridStore.add(modules);
		}
		setVisible(true);
	}

	@Override
	protected void onOk() {
		setVisible(false);
	}
}
